<template>
  <div class="add-box">
    <el-form :model="form" label-position="top">
      <el-form-item label="转账日期">
        <el-date-picker
          v-model="form.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="金额">
        <el-input v-model.number="form.money"></el-input>
      </el-form-item>
      <el-form-item label="转出账户">
        <el-select v-model="form.outAccount" value-key="id" placeholder="请选择账户类型">
          <el-option
            v-for="item in accountList"
            :key="item.id"
            :label="item.accountName"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="转入账户">
        <el-select v-model="form.inAccount" value-key="id" placeholder="请选择">
          <el-option
            v-for="item in accountList"
            :key="item.id"
            :label="item.accountName"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" :rows="2" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('changeState',false)">取 消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { transfer, findList } from '@/api/account'
import { dateFormat } from '@/utils/index'
export default {
  data() {
    return {
      form: {
        companyId: this.$store.state.user.companyId,
        date: dateFormat(new Date()),
        inAccount: '',
        outAccount: '',
        money: '',
        remark: ''
      },
      accountList: []
    }
  },
  beforeDestroy() {
    this.$off('reload')
    this.$off('changeState')
  },
  mounted() {
    this.initAccountList()
  },
  methods: {
    initAccountList() {
      findList(this.form.companyId).then(res => {
        this.accountList = res.data
      })
    },
    submit() {
      transfer(this.form).then(res => {
        if (res.success) {
          this.$emit('reload')
          this.$emit('changeState', false)
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.add-box {
  padding: 0 50px;
  //height: 500px;
  overflow-y: auto;
}
.dialog-footer {
  text-align: right;
}
</style>
